package com.wanglibao.woodtrade.woodtrade.ui.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wanglibao.woodtrade.woodtrade.R;
import com.wanglibao.woodtrade.woodtrade.ui.fragment.CommodityFragment;
import com.wanglibao.woodtrade.woodtrade.ui.fragment.SupplyByTypeFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CommodityAndSupplyActivity extends FragmentActivity implements View.OnClickListener {

    @Bind(R.id.imgbtn_commonTitle_back)
    ImageButton mImgbtnCommonTitleBack;
    @Bind(R.id.tv_chushou_title_activity)
    TextView mTvChushouTitleActivity;
    @Bind(R.id.tv_qiugou_title_activity)
    TextView mTvQiugouTitleActivity;
    @Bind(R.id.tv_title_top_activity)
    TextView mTvTitleActivity;
    @Bind(R.id.rl_title_supplyAndDemand_activity)
    RelativeLayout mRlTitleSupplyAndDemandActivity;
    @Bind(R.id.vp_supplyAndDemand_activity)
    ViewPager mVpSupplyAndDemandActivity;

    CommodityFragment commodityFragment;
    SupplyByTypeFragment supplyByTypeFragment;
    List<Fragment> mFragmentList = new ArrayList<>();
    @Bind(R.id.tv_baiban_left_activity)
    TextView mTvBaibanLeftActivity;
    @Bind(R.id.tv_baiban_right_activity)
    TextView mTvBaibanRightActivity;

    public String keyword = "";
    public String woodTypeId = "";
    public String woodTypeDetailId = "";
    public String woodTypeDetailThreePlaceId = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_commodity_and_supply);
        ButterKnife.bind(this);
        if (Build.VERSION.SDK_INT >= 19) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        } else {
            mRlTitleSupplyAndDemandActivity.setPadding(0, 0, 0, 0);
        }
        initData();
    }
    private void initData() {
        String woodTypeId = getIntent().getStringExtra("woodTypeId");
        Bundle bundle = new Bundle();
        bundle.putString("woodTypeId",woodTypeId);

        commodityFragment = new CommodityFragment();
        supplyByTypeFragment = new SupplyByTypeFragment();
        commodityFragment.setArguments(bundle);
        supplyByTypeFragment.setArguments(bundle);

        mTvChushouTitleActivity.setOnClickListener(this);
        mTvQiugouTitleActivity.setOnClickListener(this);
        mImgbtnCommonTitleBack.setOnClickListener(this);
        mFragmentList.add(commodityFragment);
        mFragmentList.add(supplyByTypeFragment);
        FragAdapter fragAdapter = new FragAdapter(getSupportFragmentManager(), mFragmentList);
        mVpSupplyAndDemandActivity.setAdapter(fragAdapter);
        mVpSupplyAndDemandActivity.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    mTvBaibanLeftActivity.setVisibility(View.VISIBLE);
                    mTvQiugouTitleActivity.setTextColor(getResources().getColor(R.color.white));
                    mTvBaibanRightActivity.setVisibility(View.INVISIBLE);
                    mTvChushouTitleActivity.setTextColor(getResources().getColor(R.color.unSelect));
                }else {
                    mTvBaibanLeftActivity.setVisibility(View.INVISIBLE);
                    mTvQiugouTitleActivity.setTextColor(getResources().getColor(R.color.unSelect));
                    mTvBaibanRightActivity.setVisibility(View.VISIBLE);
                    mTvChushouTitleActivity.setTextColor(getResources().getColor(R.color.white));
                }

            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

    }

    @OnClick(R.id.tv_commodityAndSupply_right)
    void roghtClick(){
        startActivityForResult(new Intent(this, FilterActivity.class), 100);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == 10) {
            String type = data.getStringExtra("type");
            if (type.equals("filter")) {
                woodTypeId = data.getStringExtra("woodTypeId");
                woodTypeDetailId = data.getStringExtra("woodTypeDetailId");
                woodTypeDetailThreePlaceId = data.getStringExtra("place_id");
                keyword = "";
                //加载数据
                commodityFragment.category_one_id = woodTypeId;
                commodityFragment.category_id = woodTypeDetailId;
                commodityFragment.keyWord = "";
                supplyByTypeFragment.category_one_id = woodTypeId;
                supplyByTypeFragment.category_id = woodTypeDetailId;
                supplyByTypeFragment.keyWord = "";
                commodityFragment.loadData();
                supplyByTypeFragment.loadData();
            } else if (type.equals("search")) {
                keyword = data.getStringExtra("keyword");
                woodTypeId = "";
                woodTypeDetailId = "";
                woodTypeDetailThreePlaceId = "";
                //加载数据
                commodityFragment.category_one_id = "";
                commodityFragment.category_id = "";
                commodityFragment.keyWord = keyword;
                supplyByTypeFragment.category_one_id = "";
                supplyByTypeFragment.category_id = "";
                supplyByTypeFragment.keyWord = keyword;
                commodityFragment.loadData();
                supplyByTypeFragment.loadData();
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_qiugou_title_activity:
                mTvBaibanLeftActivity.setVisibility(View.VISIBLE);
                mTvQiugouTitleActivity.setTextColor(getResources().getColor(R.color.white));
                mTvBaibanRightActivity.setVisibility(View.INVISIBLE);
                mTvChushouTitleActivity.setTextColor(getResources().getColor(R.color.text_color_didian));
                mVpSupplyAndDemandActivity.setCurrentItem(0);
                break;
            case R.id.tv_chushou_title_activity:
                mTvBaibanLeftActivity.setVisibility(View.INVISIBLE);
                mTvQiugouTitleActivity.setTextColor(getResources().getColor(R.color.text_color_didian));
                mTvBaibanRightActivity.setVisibility(View.VISIBLE);
                mTvChushouTitleActivity.setTextColor(getResources().getColor(R.color.white));
                mVpSupplyAndDemandActivity.setCurrentItem(1);
                break;
            case R.id.imgbtn_commonTitle_back:
                onBackPressed();
                break;
        }
    }

    class FragAdapter extends FragmentPagerAdapter {

        private List<Fragment> mFragments;

        public FragAdapter(FragmentManager fm, List<Fragment> fragments) {
            super(fm);
            mFragments = fragments;
        }

        @Override
        public Fragment getItem(int arg0) {
            return mFragments.get(arg0);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

    }
}
